package edu.tjut.cs.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

@Entity
public class Customer {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	private String name;

	@Embedded
	@AttributeOverride(name = "value", column = @Column(name = "email_address", unique = true))
	private Email email;

	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
	@JoinColumn(name = "customer_id")
	private Set<Address> addresses = new HashSet<Address>();

	@Embedded
	@AttributeOverrides({
		@AttributeOverride(name = "amount", column = @Column(name = "credit_limit"))
	})
	private Money creditLimit; // 信用额度

	public Customer() {
	}

	public Customer(String name, Email email, Money creditLimit) {
		this.name = name;
		this.email = email;
		this.creditLimit = creditLimit;
	}

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public Set<Address> getAddresses() {
		return addresses;
	}

	public void addAddress(Address address) {
		this.addresses.add(address);
	}
	
	public void clearAddresses() {
		this.addresses.clear();
	}
	
	public void delAddress(Address address) {
		this.addresses.remove(address);
	}
	
	public Email getEmail() {
		return email;
	}

	public Money getCreditLimit() {
		return creditLimit;
	}

	@Override
	public String toString() {
		return "Customer [name=" + name + ", email=" + email + ", creditLimit=" + creditLimit + ", addresses=" + addresses + "]";
	}
}
